﻿
#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

#endregion

namespace FasterWPF
{
    public class ScrollViewerArgs
    {
    }

    public class ScrollViewerParent : FasterWPF.IParent, FasterWPF.IScrollViewerStrategy
    {         
        //this is ANA?
        public object Generate<T>(object chain, T args, Action<ContentControl> addUIElementsAction)
        {
            return chain;
        }

        #region IParent

        //this is BIND
        public object Add<T>(object chain, string key, T args, Border itemToAdd)
        {            
            //set these here to allow overriding the default values
            ScrollViewerExt.SetCanContentScroll<FrameworkElement>((FrameworkElement)chain);
            //ScrollViewerExt.SetVerticalPageUpButtonDelayAndInterval<FrameworkElement>((FrameworkElement)chain);

            ((ScrollViewer)chain).Content = itemToAdd;
            return chain;
        }

        public void Remove(object chain, string key, Border itemToRemove)
        {
            ((ScrollViewer)chain).Content = null; //remove from Window

            IComposite ic = (IComposite)(((ScrollViewer)chain).GetIComposite<ScrollViewer>());
            ic.RemoveFromChain(itemToRemove);
        }

        public Dictionary<string, object> RetrieveDictionary(object chain)
        {
            return ((ScrollViewer)chain).GetCompositeLookupDictionary<ScrollViewer>();
        }

        //this is CATA?
        public List<Border> RetrieveCollection(object chain)
        {
            List<Border> lb = new List<Border>();
            foreach (var kvp in ((ScrollViewer)chain).GetCompositeLookupDictionary<ScrollViewer>())
            {
                lb.Add((Border)kvp.Value);
            }

            return lb;
        }

        #endregion
        
        #region ICanSort

        public void ClearCollection(object chain)
        {
        }

        public void BeforeSortCollection<T>(object chain, object args, int row, int column, FasterWPF.CommonExt.SortOrder sortOrder)
            where T : FrameworkElement
        { }

        public void BeforeSortCollection1Then2<T>(object chain, object args, int row, int column, int row2, int column2, FasterWPF.CommonExt.SortOrder sortOrder, FasterWPF.CommonExt.SortOrder sortOrder2)
  where T : FrameworkElement
        { }

        public void AfterSortCollection<T>(object chain)
            where T : FrameworkElement
        { }

        public void InnerSortCollection(object chain, int row, int column, string key, ref int counter, Border itemToReAdd)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ICanSelect

        public void SetSelected(object chain, Border itemToSetAsSelected)
        {
        }

        #endregion

        //public void GetSelected
        //    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);

        #region IScrollViewerStrategy

        public ScrollViewer GetScrollViewer(object chain)
        {
            ScrollViewer trv = (ScrollViewer)chain;
          //  var builtCheck = trv.ApplyTemplate();
          //  Border bd = (Border)trv.Template.FindName("Bd", trv);

            var settings = trv.GetSettingsDictionary();

            if (settings.Keys.Contains("ScrollViewerPadding"))
            {
                trv.Padding = (Thickness)settings["ScrollViewerPadding"];
            }
            else
            {
                trv.Padding = new Thickness(0); //default to zero
            }

            return trv;
        }

        public T GetParentContainerFromScrollViewerThumb<T>(System.Windows.Controls.Primitives.Thumb myThumb)
        {
            T source = ((Border)((ScrollViewer)
                ((Grid)
                ((ScrollBar)myThumb.TemplatedParent).Parent).TemplatedParent
                ).Parent).TemplatedParent.Cast<T>();
            return source;
        }

        #endregion
    }
}
